package com.lxq.live.security;

import com.lxq.live.dao.UserRepository;
import com.lxq.live.pojo.RoleEnum;
import com.lxq.live.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class UserSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserRepository userRepository;
    @Autowired
    LoginSuccessHandler loginSuccessHandler;
    @Autowired
    LoginFailureHandler loginFailureHandler;
    @Autowired
    UserService userService;
    /*@Autowired
    JWTLoginFilter jwtLoginFilter;
    @Autowired
    JwtTokenFilter jwtTokenFilter;*/

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(10);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //auth.inMemoryAuthentication().withUser("lxq").password("24166").roles("final");
        auth.userDetailsService(userService);
        //auth.authenticationProvider(provider);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasAuthority(RoleEnum.admin.getRole())
                .antMatchers("/user/**", "/user_zone/**", "/comment/**")
                .hasAuthority(RoleEnum.ordinary.getRole())
                .anyRequest().permitAll()
                .and()
                .formLogin().loginPage("/login.html").loginProcessingUrl("/login")
                .usernameParameter("userId").passwordParameter("password")
                .successHandler(loginSuccessHandler)
                .failureHandler(loginFailureHandler)
                .and().csrf().disable()
                .logout().logoutUrl("/logout").invalidateHttpSession(true).clearAuthentication(true);
        /*http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf().disable()
                .addFilterAt(jwtLoginFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAfter(jwtTokenFilter,JwtTokenFilter.class);*/
        //http.addFilterAt(getParameterConvert(),LoginParameterConvert.class);
    }
    /* private LoginParameterConvert getParameterConvert(){
        LoginParameterConvert convert=new LoginParameterConvert();
        try {
            convert.setAuthenticationManager(super.authenticationManager());
        } catch (Exception e) {
            e.printStackTrace();
        }
        convert.setAuthenticationSuccessHandler(loginSuccessHandler);
        return convert;
    }*/
}
